/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.action.red;

import com.mapping.Red;
import com.opensymphony.xwork2.ActionSupport;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Random;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;

/**
 *
 * @author Ricardo
 */
@Namespace(value = "/")
@Action(value = "altaRed", results = {
@Result(name = "success", location = "/GestionRed/resultadoRed.jsp")})
public class AltaRedAction extends ActionSupport{
    private Red red;
    private String nombrered;
    private String mensaje = "La Red se ha creado satisfactoriamente :";
    
    @Override
    public String execute(){       
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("inventarios");
        EntityManager em = emf.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        tx.begin();
        red = new Red();
        red.setIdRed(getIdeRed());
        red.setNombrered(nombrered);                     
        em.persist(red);        
        em.flush();
        tx.commit();
        em.close();        
        return SUCCESS;    
    }      

    private String getIdeRed() {
        String ide = "RED";
        Calendar c = new  GregorianCalendar();
        Random rnd = new Random(c.getTime().getTime());
        ide += ""+ ((int)(rnd.nextDouble()*100000));      
        return ide;
    }

    public Red getRed() {
        return red;
    }

    public void setNombrered(String nombrered) {
        this.nombrered = nombrered;
    }

    public String getMensaje() {
        return mensaje;
    }    
        
}
